package com.locationhistory.dao.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.locationhistory.utils.Constants;

/**
 * 
 * @author thuanlv1
 *
 */
public class DatabaseHandler extends SQLiteOpenHelper {
	
	protected SQLiteDatabase myDatabase;
    private  Context context;
    private String TAG = "TAG";
    
	public DatabaseHandler(Context context) {
        super(context, Constants.Database.DATABASE_NAME, null, Constants.Database.DATABASE_VERSION);
        
        this.context = context;
	}
	
    
	@Override
	public void onCreate(SQLiteDatabase db) {
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
	}
	

	/**
    * close database if necessary.
    */
   public synchronized void close()
   {
       if (myDatabase != null)
    	   myDatabase.close();
       super.close();
   }

   
   /**
	 * 
	 * this method is used to open database
	 * 
	 * @throws SQLException
	 */
	public void openDatabase() throws SQLException {
	    String myPath = Constants.Database.DATABASE_PATH + Constants.Database.DATABASE_NAME;
	    myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
	}
	
	/**
	 * This method is used to copy file db from folder access into internal
	 * store memory.
	 * 
	 */
	public void checkAndCopyDatabase() {
	    boolean dbExist = checkDataBase();
	    if (dbExist) {
	        Log.d(TAG, "database is already existed!");
	    } else {
	        this.getReadableDatabase();
	        try {
	            copyDataBase();
	        }
	        catch (IOException e) {
	            Log.d(TAG, "Error copying database");
	        }
	    }
	}
	
	
	/**
	 * 
	 * this method is used to check the exist of file db in internal store. if
	 * it's not exist,the program will copy file DB template from access folder
	 * into internal store. else don't copy.
	 * 
	 * @return
	 */
	private boolean checkDataBase() {
	    File dbFile = new File(Constants.Database.DATABASE_PATH + Constants.Database.DATABASE_NAME);
	    return dbFile.exists();
	}
	
	
	/**
	 * 
	 * this method is used to copy file db from access folder into internal
	 * store with path.if necessary.
	 * 
	 * @throws IOException
	 */
	private void copyDataBase() throws IOException {
	    InputStream myInput = context.getAssets().open(Constants.Database.DATABASE_NAME);
	    String outFileName = Constants.Database.DATABASE_PATH + Constants.Database.DATABASE_NAME;
	    OutputStream myOutput = new FileOutputStream(outFileName);
	    byte[] buffer = new byte[1024];
	    int length;
	    while ((length = myInput.read(buffer)) > 0)
	    {
	        myOutput.write(buffer, 0, length);
	    }
	    myOutput.flush();
	    myOutput.close();
	    myInput.close();
	}
	
    /**
     * this method query database by sql satements
     * 
     * @param query		satements
     * @return
     * @throws SQLException
     */
    public Cursor queryData(String query) throws SQLException {
    	Cursor cursor = myDatabase.rawQuery(query, null);
    	
        return cursor;
    }
}
